"""
File: user.py
Author: ershixiong
Date: 2023/12/21
describe:

"""


from selenium.webdriver.common.by import By

from hogwarts.Wework.pages.base_page import BasePage
from hogwarts.Wework.utils.log_utils import logger
from hogwarts.Wework.utils.utils import save_screen, ele_add_border


class UserPage(BasePage):
    _NAME = (By.CSS_SELECTOR, '[name=username]')
    _ACCOUNT = (By.CSS_SELECTOR, '[name=acctid]')
    _MOBILE = (By.CSS_SELECTOR, '[name=mobile]')
    _EMAIL = (By.CSS_SELECTOR, '#memberAdd_mail')
    _SAVE_BOTTON = (By.XPATH, '//*[text()="保存"]')

    def add(self, name, account, mobile, email):
        logger.debug(f'添加用户，参数为：name：{name}，account：{account}，email：{email}')

        try:
            if self.element_visible(self._NAME):
                # 进入页面后截图
                save_screen(self.driver)

                self.find_and_sendkeys(self._NAME, name)
                self.find_and_sendkeys(self._ACCOUNT, account)
                self.find_and_sendkeys(self._MOBILE, mobile)
                self.find_and_sendkeys(self._EMAIL, email)

                # 点击保存之前,添加红框并截图
                ele_btn = self.find(self._SAVE_BOTTON)
                ele_add_border(self.driver, ele_btn)
                save_screen(self.driver)
                self.find_and_click(self._SAVE_BOTTON)

                from hogwarts.Wework.pages.contact_page import ContactPage
                return ContactPage(self.driver)
        except Exception as e:
            logger.debug(e)
            logger.debug('未进入添加页面')
